package com.example.demo2.student;


import com.example.demo2.Util.DBHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/AddClasss")
public class AddClasss extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int i;
		DBHelper dbHelper = new DBHelper();
		 conn = dbHelper.getConn();
		try {
			preparedStatement = conn.prepareStatement("insert into webdesign.class(className) VALUE (?)");
			preparedStatement.setString(1,name);
			 i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			dbHelper.closeAll(conn,preparedStatement,resultSet);
		}

		request.setAttribute("list", i);
		request.getRequestDispatcher("/addClasss.jsp").forward(request, response);
	}

}
